home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Random / Commodore 64c / SOURCE / Traps.c < prev    next >
Text File  |  1994-02-13  |  3KB  |  88 lines

  1. /*
  2.     Commodore 64 Emulator v0.1      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "Memory.h"
  20. #include "Error.h"
  21. #include "Traps.h"
  22. #include "Registers.h"
  23.  
  24. #define MAXTRAPS 16
  25. static trap trapList[MAXTRAPS];
  26. static byte numTraps;
  27.  
  28. void TrapInitialize()
  29. {
  30.     numTraps=0;
  31. }
  32.  
  33. int AddTrap(trap t)
  34. {
  35.     /* If we are at out limit for traps, then internal error */
  36.     if (numTraps==MAXTRAPS) InternalError(kTrapStorageOverflow);
  37.     
  38.     /* Check to see if the ROMs and the trap agree */
  39.     if (t.check[0]!=ByteAt(t.addr)) return kWrongROMError;
  40.     if (t.check[1]!=ByteAt(t.addr+1)) return kWrongROMError;
  41.     if (t.check[2]!=ByteAt(t.addr+2)) return kWrongROMError;
  42.     
  43.     /* Add this trap to out list */
  44.     trapList[numTraps++]=t;
  45.     
  46.     /* Either modify the low ROM or high ROM, as per address specified */
  47.     if (t.addr < 0xc000) loROM[t.addr&0x1fff]=0xff;
  48.     else hiROM[t.addr&0x1fff]=0xff;
  49.  
  50.     return kNoError;
  51. }
  52.  
  53. int DeleteTrap(word addr)
  54. {
  55.     int x, y;
  56.     
  57.     /* Search through traps added for the address to remove from */
  58.     for (x=0; x<numTraps; x++)
  59.         if (trapList[x].addr==addr) {
  60.         
  61.             /* Fix ROM to its initial state */
  62.             if (addr <= 0xc000) loROM[addr&0x1fff]=trapList[x].check[0];
  63.             else hiROM[addr&0x1fff]=trapList[x].check[0];
  64.             
  65.             /* Shift all of the traps down one slot */
  66.             for (y=x; y<numTraps-1; y++) trapList[y]=trapList[y+1];
  67.             
  68.             numTraps--;
  69.             return kNoError; }
  70.     
  71.     /* Attempted to remove a trap not added */
  72.     InternalError(kTrapNotInstalled);
  73. }
  74.  
  75. void TrapExecute()
  76. {
  77.     int x;
  78.     extern void nonimp(void);
  79.     
  80.     /* Search for a trap at the current pc location, and if so execute it */
  81.     for (x=0; x<numTraps; x++)
  82.         if (trapList[x].addr==(pc-1)) {
  83.             (*trapList[x].call)();
  84.             return; }
  85.     /* This is not OUR trap code, so it's a nonimplemented instruction */
  86.     ini();
  87. }
  88.